/*
*
*AuthInterceptor
*添加header认证
* */
import 'dart:io';

import 'package:base/base_lib.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '/app/app_controller.dart';

class AuthInterceptor extends Interceptor {
  var appController = Get.put(AppController());

  // var mmkv = MMKV.defaultMMKV();
  String PLATFORM = "android"; //可根据代码进行判断
  int platformID = 2; //可根据代码进行判断
  @override
  void onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    //获取app版本
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    String version = packageInfo.version;
    if (Platform.isIOS) {
      PLATFORM = "ios";
      platformID = 1;
    } else if (Platform.isAndroid) {
      PLATFORM = "android";
      platformID = 2;
    } else if (Platform.isWindows) {
      PLATFORM = "Windows";
      platformID = 3;
    } else if (Platform.isMacOS) {
      PLATFORM = "macos";
      platformID = 4;
    } else if (Platform.isLinux) {
      PLATFORM = "Linux";
      platformID = 7;
    }

    Map<String, String> headers = {};
    headers["Accept-Charset"] = "utf-8";
    headers["Connection"] = "keep-alive";
    headers["Accept"] = "*/*";
    headers["x-version"] = version; //自己更改配置
    headers["platform"] = PLATFORM;
    headers["platformID"] = "$platformID";
    headers["eco-language"] = appController.getLocale()!.languageCode;
    //获取存储数据 保存header token
    UserInfoModel? userInfoModel = UserInfoManager.shared.info();

    if (userInfoModel != null) {
      headers["Authorization"] =
          "${userInfoModel.tokenType} ${userInfoModel.token}"; //添加自己项目中的请求头 进行保存
    }

    options.headers = headers;
    return super.onRequest(options, handler);
  }
}
